<!DOCTYPE html>
<html>
<head>
    <title locale="page/title">Authentication Required</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
    <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
    <script type="text/javascript" src="../../script/jquery.min.js"></script>
    <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
    <script type="text/javascript" src="../../script/ao_module.js"></script>
    <script type="text/javascript" src="../../script/applocale.js"></script>
    <style>
        .topSlate{
            background-image: url("img/auth_slate.png");
            background-repeat: no-repeat;
            background-repeat: no-repeat !important;
            background-size: 100% auto;
            background-attachment: fixed !important;
            background-color: #1a1a1a;
            height: 70px; 
            margin-bottom: 4px;
        }

        .whitefont{
            color: white !important;
        }
    </style>
</head>
<body>
    <div class="topSlate">
        <div class="ui container">
            <h3 class="ui header" style="padding-top: 12px;">
                <div class="content whitefont">
                    <span id="title">Authentication Required</span>
                    <div id="desc" class="sub header whitefont">in order to proceed this operation</div>
                </div>
            </h3>
        </div>
    </div>
    <div class="ui container">
        <div class="ui left icon small fluid input" style="margin-top: 12px;">
            <input id="username" type="text" placeholder="Username">
            <i class="user icon"></i>
        </div>
        <div class="ui left icon fluid input" style="margin-top: 12px;">
            <input id="password" type="password" placeholder="Password">
            <i class="key icon"></i>
        </div>
        <br>
        <div align="right">
            <button class="ui black button" locale="button/confirm" onclick="confirm()">Confirm</button>
            <button class="ui button" locale="button/cancel" onclick="cancel()">Cancel</button>
        </div>
        
    </div>
    
    <script>
        /*
            Usage Example:
            var apiObject = {
                api: "system/myfunction",
                data: {message: "Hello World"},
                title: "<i class='red exclamation triangle icon'></i> Danger Operation <i class='red exclamation triangle icon'></i>",
                desc: "Please enter your password to confirm operation.",
                thisuser: true, //This username as default, set to false for allowing other user name to be entered
                method: "GET",
                success: "systemAO/myfunction/succ.html" //Redirection if success
            }

            ao_module_newfw({
                url: "SystemAO/security/authreq.html#" + encodeURIComponent(JSON.stringify(apiObject)),
                width: 480,
                height: 300,
                appicon: "SystemAO/security/img/lock.svg",
                title: "Authentication Required",
                parent: ao_module_windowID,
                callback: "handleAuthCallback"
            });

        */
       ao_module_setFixedWindowSize();
       ao_module_setWindowSize(420, 260);
       var actionObject = {};
       var method = "GET";
       if (window.location.hash.length > 1){
            var object = JSON.parse(decodeURIComponent(window.location.hash.substr(1)));
            console.log(object);

            if (typeof(object.title) != "undefined"){
                $("#title").html(object.title);
            }

            if (typeof(object.desc) != "undefined"){
                $("#desc").html(object.desc);
            }

            if (typeof(object.thisuser) != "undefined" && object.thisuser == true){
                //Load user info from server side
                $.get("../../system/desktop/user", function(data){
                    $("#username").val(data.Username);
                    $("#username").attr("readonly","true");
                    $("#username").parent().addClass("disabled");
                });
            }

            if (typeof(object.method) != "undefined" && object.method != ""){
                method = object.method;
            }

            actionObject = object;
       }else{
           //Invalid usage
       }


       function confirm(){
        $("password").parent().removeClass("error");
            var payload = actionObject.data;
            //Append custom payload to the original payload
            payload.username = $("#username").val();
            payload.password = $("#password").val();
            //Request endpoint
            $.ajax({
                url: "../../" + actionObject.api,
                data: payload,
                method: method,
                success: function(data){
                    if (data.error != undefined){
                        //callback to parent with error code
                        if (ao_module_hasParentCallback()){
                            ao_module_parentCallback(data);
                        }

                        ao_module_close();
                    }else{
                        if (ao_module_hasParentCallback()){
                            ao_module_parentCallback(data);
                        }

                        if (actionObject.success != undefined){
                            window.top.location = "../../" + actionObject.success;
                        }

                        ao_module_close();
                    }
                    
                }, error: function(){
                    $("#password").parent().addClass("error");
                    if ($("#username").parent().hasClass("disabled") == false){
                        $("#username").parent().addClass("error");
                    }
                }
            })
       }

       function cancel(){
            if (ao_module_hasParentCallback()){
                ao_module_parentCallback(false);
            }

            ao_module_close();
       }

       if (applocale){
            //Applocale found. Do localization
            applocale.init("../locale/authreq.json", function(){
                applocale.translate();
            });
        }else{
            //Applocale not found. Is this a trim down version of ArozOS?
            var applocale = {
                getString: function(key, original){
                    return original;
                }
            }
        }
    </script>
</body>
</html>